package com.example.dogliving.fragment.home.itemhomefragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.dogliving.R;
import com.example.dogliving.activity.BannerActivity;
import com.example.dogliving.activity.BannerrActivity;
import com.example.dogliving.helper.GlideImageLoader;
import com.example.dogliving.utils.Constant;
import com.example.dogliving.view.IHomeView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerClickListener;

import java.util.ArrayList;
import java.util.List;

public class ShowFragment extends Fragment implements IHomeView {
    private Context mContext;
    private List<String> mList_URL=new ArrayList<>();
    private Banner fragmentBanner;
    private  String url=null;
    private Intent intent;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.item_fragment_home_show,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        fragmentBanner= (Banner) view.findViewById(R.id.item_fragment_home_show_banner);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initBanner();
    }

    private void initBanner() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mList_URL.add(Constant.Live_URL1);
                mList_URL.add(Constant.Live_URL2);
                fragmentBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
                fragmentBanner.setImageLoader(new GlideImageLoader());
                //设置图片集合
                fragmentBanner.setImages(mList_URL);
                //设置标题集合（当banner样式有显示title时）
                fragmentBanner.setBannerTitles(mList_URL);
                //设置自动轮播，默认为true
                fragmentBanner.isAutoPlay(true);
                //设置轮播时间
                fragmentBanner.setDelayTime(1500);
                //设置指示器位置（当banner模式中有指示器时）
                fragmentBanner.setIndicatorGravity(BannerConfig.CENTER);
                //banner设置方法全部调用完毕时最后调用
                fragmentBanner.setOnBannerClickListener(new OnBannerClickListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        Log.i("TAG", "OnBannerClick:====== "+position);
                        if (position==1){
                            intent = new Intent(mContext, BannerActivity.class);
                        }else {
                            intent = new Intent(mContext, BannerrActivity.class);
                        }
                        mContext.startActivity(intent);
                    }
                });
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        fragmentBanner.start();
                    }
                });
            }

        }).start();
    }

    @Override
    public void toSetAdapter(HomeAdapter adapter) {

    }

    @Override
    public void showError(Exception e) {

    }
}
